/**
 * 最长无重复子串
 */
const s = 'abcabcbfb'
const s1 = 'aaaaaaaa'

// 直接利用set的唯一性， 可以办到只存不重复的

function S2(str) {
    let i = 0, j = 0, maxSize = 0
    let set = new Set()
    while (i < str.length) {
        if (set.has(str[i])) {
            set.delete(str[i])
            set.add(str[i])
            console.log(set)
            maxSize = Math.max(set.size, maxSize)
        } else {
            set.add(str[i])
        }
        i++
    }
    return maxSize
}

// console.log(S2(s));
// console.log(S2(s1));

// TODO：复习
// 数组实现, 滑动窗口
function S3(str) {
    let arr = [], i = 0, maxSize = 0
    for (let j = 0; j < str.length; j++) {
        const index = arr.indexOf(str[j])
        if (index !== -1) {
            arr.splice(0, index + 1)
        }
        arr.push(str[j])

        maxSize = Math.max(maxSize, arr.length)
    }
    console.log(maxSize)
    return maxSize
}

S3(s)


function longestNoDupString(str){
    let res = [], maxSize=0, len = str.length
    for(let i=0; i < len; i++){
        if(!res.includes(str[i])){
            res.push(str[i])
        }else {
            res.splice(res.indexOf(str[i])+1, 1)
            res.push(str[i])
        }
        maxSize = Math.max(maxSize, res.length)
    }
}

function long(str) {
    let res = [], len = str.length, maxSize= 0
    for(let i=0; i<len; i++){
        if(res.includes(str[i])){
            res.splice(res.indexOf(str[i])+1, 1)
        }
        res.push(str[i])
        maxSize = Math.max(maxSize, res.length)
    }
    return res;
}
